package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.StrategyGrandSonComment;
import cn.wolfcode.trip.base.domain.StrategySonComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyGrandSonCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategySonCommentMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyGrandSonCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyGrandSonCommentServiceImpl implements IStrategyGrandSonCommentService {

    @Autowired
    private StrategyGrandSonCommentMapper strategyGrandSonCommentMapper;
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private StrategySonCommentMapper strategySonCommentMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public void saveOrUpdate(StrategyGrandSonComment strategyGrandSonComment) {
        //添加二级子评论
        strategyGrandSonComment.setCreateTime(new Date());
        //判断二级评论的回复类型
        if (strategyGrandSonComment.getStrategyGrandSonComment() != null) {
            //二级评论类型为内回复,根据id查询父的一级评论,手动添加一级评论
            Long grandSonId = strategyGrandSonComment.getStrategyGrandSonComment().getId();
            StrategySonComment strategySonComment=strategySonCommentMapper.selectByGrandSonCommentId(grandSonId);
            strategyGrandSonComment.setStrategySonComment(strategySonComment);
            //查询被回复者
            User replyUser=userMapper.selectByStrategyGrandSonCommentId(grandSonId);
            strategyGrandSonComment.setReplyUser(replyUser);
        }
        //添加二级评论
        strategyGrandSonCommentMapper.insert(strategyGrandSonComment);
        //增加评论数
        StrategyComment strategyComment = strategyCommentMapper.selectByPrimaryKey(
                strategyGrandSonComment.getStrategySonComment().getParent().getId());
        Integer newViewCount = strategyComment.getViewCount()+1;
        strategyCommentMapper.updateViewCount(newViewCount,strategyComment.getId());
    }


}
